﻿Sw.Size = Class({
    width: null,
    height: null,
    initialize: function(width, height) {
        this.width = width;
        this.height = height;
    },
    add: function(width, height) {
        return new Sw.Size(this.width + width, this.height + height);
    },
    applyTo: function(element) {
        $(element).width(this.width).height(this.height);
    },
    applyHeightTo: function(element) {
        $(element).height(this.height);
    },
    applyWidthTo: function(element) {
        $(element).width(this.width);
    }
    
});
Sw.apply(Sw.Size, {
    SizeMode: {
        /**
        * 指定默认的大小表达方式
        * @property
        */
        Default: 0,
        /**
        * 指定通过内边距（包括padding，不包括border和margin）表示大小
        * @property
        */
        Inner: 1,
        /**
        * 指定通过外边距（包括padding、border和margin）表示大小
        * @property
        */
        Outer: 2
    },
    fromElement: function(element, mode) {
        mode = mode || this.SizeMode.Default;
        element = $(element);
        switch (mode) {
            case this.SizeMode.Inner:
                return new Sw.Size(
                element.innerWidth(), element.innerHeight());
            case this.SizeMode.Outer:
                return new Sw.Size(
                element.outerWidth(true), element.outerHeight(true));
            default:
                return new Sw.Size(
                element.width(), element.height());
        }
    },
    fromArray: function(array, mode) {
        var wid = 0, hei = 0;
        $(array).each(function(index, element) {
           wid += Sw.Size.fromElement(element, mode).width;
           hei += Sw.Size.fromElement(element, mode).height;
        });
        var size = new Sw.Size(wid, hei);
        return size;
    }
});
Sw.Offset = Class(/** @lends ExUI.Offset# */{
left: null,
top: null,
initialize: function(left, top) {
    this.left = left;
    this.top = top;
},
add: function(left, top) {
    return new Sw.Offset(this.left + left, this.top + top);
},
applyTo: function(element) {
    $(element).css('left', this.left).css('top', this.top);
}
});
Sw.apply(Sw.Offset, {
    fromElement: function(element) {
        var offset = $(element).offset();
        return new Sw.Offset(offset.left, offset.top);
    }
});
Sw.Bound = Class(/** @lends ExUI.Bound# */{
    top: null,
    right: null,
    bottom: null,
    left: null,
    initialize: function(top, right, bottom, left) {
        this.top = top;
        this.right = right;
        this.bottom = bottom;
        this.left = left;
    }
});
Sw.apply(Sw.Bound, {
    fromElement: function(element, mode) {
        var size = Sw.Size.fromElement(element, mode);
        var offset = Sw.Offset.fromElement(element);
        return new Sw.Bound(
        offset.top,
        offset.left + size.width,
        offset.top + size.height,
        offset.left
        );
    }
});
/**
* @class 键盘按键列表
*/
Sw.keyCode = {
    /**
    * 回退键
    * @property
    * @static
    * @type int
    */
    Backspace: 8,

    /**
    * Tab键
    * @property
    * @static
    * @type int
    */
    Tab: 9,

    /**
    * Return键
    * @property
    * @static
    * @type int
    */
    Return: 13,

    /**
    * ESC键
    * @property
    * @static
    * @type int
    */
    Esc: 27,

    /**
    * 左方向键
    * @property
    * @static
    * @type int
    */
    Left: 37,

    /**
    * 上方向键
    * @property
    * @static
    * @type int
    */
    Up: 38,

    /**
    * 右方向键
    * @property
    * @static
    * @type int
    */
    Right: 39,

    /**
    * 下方向键
    * @property
    * @static
    * @type int
    */
    Down: 40,

    /**
    * DEL键
    * @property
    * @static
    * @type int
    */
    Delete: 46
};